#!/bin/bash
# Docker容器启动脚本

set -e

# 打印启动信息
echo "🚀 Starting MySQL-to-KWDB Migration Tool"
echo "📅 $(date)"
echo "🐍 Python version: $(python3 --version)"
echo "💻 Container info: $(uname -a)"

# 设置日志级别
export LOG_LEVEL=${MIGRATION_LOG_LEVEL:-INFO}
echo "📊 Log level: $LOG_LEVEL"

# 创建必要的目录
mkdir -p /app/logs /app/data /app/output /app/reports
echo "📁 Created necessary directories"

# 检查环境变量
echo "🔧 Environment configuration:"
echo "  - Max workers: ${MIGRATION_MAX_WORKERS:-4}"
echo "  - Log level: ${MIGRATION_LOG_LEVEL:-INFO}"

# 如果是API模式，启动API服务器
if [ "$1" = "api" ] || [ "$1" = "--api" ]; then
    echo "🌐 Starting API server..."
    exec python3 -m src.api --host 0.0.0.0 --port 8080
fi

# 如果是监控模式，启动监控服务
if [ "$1" = "monitor" ] || [ "$1" = "--monitor" ]; then
    echo "📊 Starting monitoring dashboard..."
    # 这里可以启动一个简单的监控界面
    while true; do
        echo "📈 System Status at $(date):"
        echo "  Memory: $(free -h | grep Mem | awk '{print $3 "/" $2}')"
        echo "  Disk: $(df -h /app | tail -1 | awk '{print $3 "/" $2 " (" $5 " used)"}')"
        echo "  Load: $(uptime | awk -F'load average:' '{print $2}')"
        sleep 30
    done
fi

# 如果是测试模式，运行测试
if [ "$1" = "test" ] || [ "$1" = "--test" ]; then
    echo "🧪 Running tests..."
    exec python3 -m pytest tests/ -v
fi

# 如果是基准测试模式
if [ "$1" = "benchmark" ] || [ "$1" = "--benchmark" ]; then
    echo "⚡ Running performance benchmarks..."
    exec python3 -m pytest tests/test_performance_benchmark.py -v -s
fi

# 默认情况下，运行迁移工具
echo "🔄 Running migration tool with args: $@"
exec python3 migrate.py "$@"
